﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using AssetAllocationSystem.Models;
using AssetAllocationSystem.Repositories;
using AssetAllocationSystem.Tests.RepositoriesFixtures;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;

namespace AssetAllocationSystem.Tests.Repositories
{
    /// <summary>
    /// Summary description for CompanyRepositoryFixture
    /// </summary>
    [TestClass]
    public class CompanyRepositoryFixture : FixtureBase
    {
        private static ISession session;
        public CompanyRepositoryFixture()
        {
            //
            // TODO: Add constructor logic here
            //
            session = CreateSession();
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
         [ClassInitialize()]
         public static void MyClassInitialize(TestContext testContext)
         {
             var allocationRepository = new CompanyRepository(session);
             
             for (int i = 1; i <= 5; i++)
             {
                 var company = new Company();
                 company.Id = Guid.NewGuid();
                 company.Name = "Company1" + i;
                 company.Assets = null;
                 company.Employees = null;

                 session.Save(company);
             }

             session.Flush();
         }
        
         //Use ClassCleanup to run code after all tests in a class have run
         [ClassCleanup()]
         public static void MyClassCleanup()
         {
             var companyRepository = new CompanyRepository(session);
             var companies = companyRepository.FindAll();
             
             foreach (var company in companies)
             {
                 session.Delete(company);
             }

             session.Flush();
         }
        
         //Use TestInitialize to run code before running each test 
         [TestInitialize()]
         public void MyTestInitialize() { }
        
         //Use TestCleanup to run code after each test has run
         [TestCleanup()]
         public void MyTestCleanup() { }
        
        #endregion

        [TestMethod]
        public void Check_if_FindByName_Works_well_with_Like_criteria()
        {
            var companyRepository = new CompanyRepository(session);
            var companies = companyRepository.FindByName("Company1", false);

            Assert.IsNotNull(companies);
        }

        [TestMethod]
        public void Check_if_FindByName_works_well_with_exact_criteria()
        {
            var companyRepository = new CompanyRepository(session);
            var companies = companyRepository.FindByName("Company12", true);

            Assert.IsNotNull(companies);
            Assert.AreEqual(1, companies.Count);
        }
    }
}
